﻿using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

namespace visualtwimilar
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class MainWindow
  {
    private readonly MainViewModel _viewModel;
    private readonly Dictionary<UserShape, UIElement> _hlinks = new Dictionary<UserShape, UIElement>();

    public MainWindow()
    {
      InitializeComponent();
      _viewModel = new MainViewModel();
    }

    void ViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      SetStatus(e);
    }

    private void SetStatus(PropertyChangedEventArgs e)
    {
      if (e.PropertyName == "Users")
      {
        foreach (var user in _viewModel.Users)
        {
          if (!_hlinks.Keys.Contains(user))
          {
            _hlinks.Add(user, GetHyperlink(user));
            LayoutRoot.Children.Add(_hlinks[user]);
          }
        }
      }
    }

    private static UIElement GetHyperlink(UserShape user)
    {
      var text = new Run(user.Name);
      var hyperLink = new Hyperlink(text)
                        {
                          Command = new NavigateTo(user.Name)
                        };
      var textBlock = new TextBlock(hyperLink);

      textBlock.SetBinding(Canvas.LeftProperty, new Binding("Left") { Source = user });
      textBlock.SetBinding(Canvas.TopProperty, new Binding("Top") { Source = user });

      return textBlock;
    }

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {

      LayoutRoot.Children.Clear();
      _viewModel.Initiallize("butaji", ViewModelPropertyChanged,
        LayoutRoot.RenderSize.Width,
        LayoutRoot.RenderSize.Height);
    }

  }
}
